package cn.bdqn.controller;

import cn.bdqn.domain.Vehicle;
import cn.bdqn.service.VehicleService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 车辆控制层
 * @author shaojie
 */
@RestController
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    /**
     * 根据id查询
     * @param id
     * @return
     **/
    // ------------------- 服务熔断-----------------
    @GetMapping(value = "/vehicle/queryByPrimaryKey/{id}")
    public Result queryVehicleByPrimaryKey(@PathVariable("id") Integer id) {
        try{
            return ResultGenerator.genSuccessResult(vehicleService.queryVehicleByPrimaryKey(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 选择更新
     * @param record
     */
    // ------------------- 服务熔断-----------------
    @RequestMapping(value = "/vehicle/updateByPrimaryKeySelective/record")
    public Result updateByPrimaryKeySelective(@RequestBody Vehicle record){
        try {
            vehicleService.updateByPrimaryKeySelective(record);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("更新失败");
        }
    }

    /**
     * 新增
     * @param record
     */
    // ------------------- 服务熔断-----------------
    @RequestMapping(value = "/vehicle/insert/record")
    public Result insert(@RequestBody Vehicle record){
        try {
            vehicleService.insert(record);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("新增失败");
        }
    }

    /**
     * 多条件查询   分页
     *
     * @param pageNum   当前页
     * @param pageAmount    每页显示的页数
     * @return
     */
    // ------------------- 服务熔断-----------------
    @PostMapping(value = "/vehicle/queryByVehicle/vehicle/pageNum/{pageNum}/pageAmount/{pageAmount}")
    public Result queryByVehicle(@RequestBody Vehicle vehicle,@PathVariable("pageNum") Integer pageNum,@PathVariable("pageAmount") Integer pageAmount) {

        try {
            Page page = vehicleService.queryByVehicle(vehicle,pageNum,pageAmount);
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

}
